 <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ include file="/WEB-INF/component/commonTagLib.jsp"%>
<!DOCTYPE html>
<html>
<head>
<title><spring:message code="message.lable.DICjiXingChaKan"/></title>
<%@ include file="/WEB-INF/component/commonCSS.jsp"%>
<!--[if lt IE 9]>
    <script src="lib/js/html5shiv.min.js"></script>
    <script src="lib/js/respond.min.js"></script>
<![endif]-->
</head>
<body class="skin-blue-light sidebar-mini fixed skin-blue-light-frame">
    <section class="content-header">
        <h1><spring:message code="message.lable.DICjiXingChaKan"/></h1>
        <ol class="breadcrumb">
            <li><a href="${ctx}/home.jsp"><i class="fa fa-home"></i><spring:message code="message.lable.first"/></a></li>
            <li class="active"><a href="#"><spring:message code="message.lable.DicManage"/></a></li>
            <li class="active"><a href="#"><spring:message code="message.lable.drugformDic"/></a></li>
        </ol>
    </section>
    <section class="content">
        <div class=" box box-success">
            <div class="box-body">
            <form action="${ctx}/dicDrugform/exportExcel.html" id="exportExcel" method="post">
            <input id="fileName" name="fileName" type="hidden" value="<spring:message code="message.lable.drugformxls"/>" />
            <div class="input">
            
                	<div class="textalign1"><spring:message code="message.listForm.JiXingMingCheng"/>：</div>
                    <div class="boxinput">
                        <input type="text" class="textinput" id="txtDrugForm" name="formName" placeholder="<spring:message code="message.listForm.JiXingMingCheng"/>" />
                    </div>
                	
                    <div class="textalign1"><spring:message code="message.listForm.JiXingBianMa"/>：</div>
                    <div class="boxinput">
                        <input type="text" class="textinput" id="txtDrugName" name="formCode" placeholder="<spring:message code="message.listForm.JiXingBianMa"/>" />
                    </div>
                    
                    <div class="textalign1"><spring:message code="message.lable.status"/>：</div>
                    <div class="boxinput">
                        <select id="status" name="status" class="textinput">
                            	<option value="" ><spring:message code="message.select.option"/></option>
	                            <option value="1" ><spring:message code="message.button.start"/></option>
	                            <option value="0" ><spring:message code="message.button.stop"/></option>
                            </select>
                    </div>
                   <div class="textalign" >
                    <button type="button" onclick="likeQuery();" class="btn btn-success btn-sm" id="selectDicCompType"> <spring:message code="message.button.seachSpacing"/></button>
                	</div>
                </div>
                </form>
            </div>
        </div>
       <table class="jqgrid" id="gridlist"></table>
       <div id="gridpage"></div>
       <div class=" box box-warning bottongroup">
            <ms:btn url="/dicDrugform/add.html"><label id="addNew" class="btn btn-default btn-sm"><spring:message code="message.button.addSpacing"/></label></ms:btn>
            <ms:btn url="/dicDrugform/enable.html"><label id="allStart" class="btn btn-default btn-sm"><spring:message code="message.button.startSpacing"/></label></ms:btn>
            <ms:btn url="/dicDrugform/disable.html"><label id="allDelete" class="btn btn-default btn-sm"><spring:message code="message.button.stopSpacing"/></label></ms:btn>
			<label onclick="exportExcel();" class="btn btn-default btn-sm"><spring:message code="message.lable.daoChu"/></label>
       </div>
      
        <div class="modal fade" id="exampleModal" tabindex="-1">
            <div class="modal-dialog">
                <div class="modal-content">
                    <div class="modal-header bg-primary">
                        <button type="button" class="close" data-dismiss="modal" ><span >&times;</span></button>
                        <h5 class="modal-title" id="exampleModalLabel"><spring:message code="message.lable.DICjiXingXinZeng"/></h5>
                    </div>
                    <div class="modal-body">
                  <form>
                    <div class="form-group form-group-xs">
                        <label for="recipient-name" class=" control-label"><spring:message code="message.listForm.yiJiJiXingBianMa"/>：</label>
                        <div class="">
                        	<input type="text" class="form-control"  id="txtOneID" placeholder="<spring:message code="message.listForm.yiJiJiXingBianMa"/>" maxlength="10">
                        </div>
                    </div>
                    <div class="form-group form-group-xs">
                        <label for="recipient-name" class=" control-label"><spring:message code="message.listForm.yiJiJiXingMingCheng"/>：</label>
                        <div class="">
                        	<input type="text" class="form-control"  id="txtOneName" placeholder="<spring:message code="message.listForm.yiJiJiXingMingCheng"/>" maxlength="50">
                        </div>
                    </div>
                   <div id="addTwoForm" style="display:none">
                    <button type="button" id="addDouble" class="btn btn-primary btn-sm"><spring:message code="message.listForm.tianJiaErJiJiXing"/></button>
                    </div>
                    <div id="twoForm" style="display:none">
                    <div class="form-group form-group-xs">
                        <label for="recipient-name" class="control-label"><spring:message code="message.listForm.erJiJiXingBianMa"/>：</label>
                        <div class="">
                            <input type="text" class="form-control" id="txtTwoId" placeholder="<spring:message code="message.listForm.erJiJiXingBianMa"/>">
                        </div>
                    </div>
                    <div class="form-group form-group-xs">
                        <label for="recipient-name" class=" control-label"><spring:message code="message.listForm.erJiJiXingMingCheng"/>：</label>
                        <div class="">
                            <input type="text" class="form-control" id="txtTwoName" placeholder="<spring:message code="message.listForm.erJiJiXingMingCheng"/>">
                        </div>
                   </div>
                    </div>
                </form>
            		</div>
              <div class="modal-footer">
              <input hidden="true" id="spid"/>
              <input hidden="true" id="spfatherid"/>
                  <button type="button" onclick="save();" class="btn btn-primary btn-sm"><spring:message code="message.button.saveSpacing"/></button>
                  <button type="button" class="btn btn-danger btn-sm" data-dismiss="modal"><spring:message code="message.button.closeSpacing"/></button>
              </div>
            </div>
         </div>
      </div>
    </section>
    <%@ include file="/WEB-INF/component/commonJS.jsp"%>
    <script type="text/javascript">
  //绑定回车
    $('#txtDrugName,#txtDrugForm,#status').bind('keydown', function(event) {
        if (event.keyCode == "13") {
            $('#selectDicCompType').click();
        }
    });	
    	var type = 0;//1新增 ，2修改，3新增二级剂型 
    	var editType=0;//1修改一级剂型，2修改二级剂型
    	var flag = false;

	    
		//停用、启用
		function batchChangeStatus(status) {
			var msg = "";
	         var url = ""; 
        	if (status == 1){
        		msg = "<spring:message code="message.button.start"/>";
        		url = "${ctx}/dicDrugform/disable.html";
        	} else if (status == 0){
        		msg = "<spring:message code="message.button.stop"/>";
        		url = "${ctx}/dicDrugform/enable.html";
        	}
			var rowIds = $("#gridlist").jqGrid("getGridParam", "selarrrow") || [];
			if (rowIds.length <= 0) {
				$.alert("<spring:message code="message.lable.pleaseSelectOperational"/><spring:message code="message.listForm.jiXing"/><spring:message code="message.yanZheng.ganTanHao"/>", "warn");
				return;
			}
			
			var str = "";
        	var names = "";
        	var count = 0;
        		for (var i = 0;i < rowIds.length; i++){
        			var rowdata = $("#gridlist").jqGrid('getRowData', rowIds[i]);
        			
            		if (status == 1){
            			if (rowdata.status == "<spring:message code="message.button.stop"/>"){
            				names += rowdata.formName+",";
            				str += rowIds[i]+",";
            				count++;
            			}
            		} else if (status == 0) {
            			if (rowdata.status == "<spring:message code="message.button.start"/>"){
            				names += rowdata.formName+",";
            				str += rowIds[i]+",";
            				count++;
            			}
            		}
            	}
        		if (names != ""){
        			names = names.substring(0,names.length-1);
        		} else {
        			$.alert("<spring:message code="message.lable.pleaseSelect"/>"+msg+"<spring:message code="message.lable.OKData"/>", "warn");
            		return;
        		}
			
        		  $.HN.message.confirm('<spring:message code="message.lable.OKYES"/>'+msg+'<spring:message code="message.lable.FlowerDataYESorNo"/><br/>'+names, '', '').on(function (e) {
	            if (e) {
	            	if (str.length > 0){
                		str = str.substring(0,str.length-1);
                	}
			var list = [];
			for (var i=0; i<rowIds.length; i++) {
				rowId = rowIds[i];
				list.push({"formId":rowId, "status":status});
			}

			$.post(url, {
				"array" : json.jsonToString(list)
			}, function(result) {
				if (result.success) {
					$.alert("<spring:message code="message.HN.alert.chengGong"/>"+msg+''+count+"<spring:message code="message.HN.alert.tiaoShuJu"/>", "success");
					likeQuery($("#gridlist").jqGrid('getGridParam','page'));
					 $("#gridlist").trigger("reloadGrid"); 
					/* gridSerch($("#gridlist").jqGrid('getGridParam','page')); */
				} else {
					$.alert(result.msg || "<spring:message code="message.HN.alert.fail"/>", "error");
				}
			}, "json");
	            }
			});
		}
		
		
		
		
		
        $(function () {
        	
        	$("#txtDrugForm").focus();//加载页面光标在第一个查询框内
        	
        	$('#allDelete').click(function(){
        		batchChangeStatus(0);
        	});
        	$('#allStart').click(function(){
        		batchChangeStatus(1);
        	});
        	
            $('#addNew').click(function () {
            	flag=false;
            	type=1;
            	$("#txtOneID").attr("readonly",false);
            	$("#txtOneName").attr("readonly",false);
            	$("#addTwoForm").show();
            	$("#twoForm").hide();
            	$("#spid").val("");
            	$("#spfatherid").val("0");
            	$('.modal-title').html('<spring:message code="message.lable.DICjiXingXinZeng"/>');
            	$('#txtOneID').val("");
				$('#txtOneName').val("");
				$('#txtTwoId').val("");
				$('#txtTwoName').val("");
                $('#exampleModal').modal({
                    keyboard: true
                });
            });
		  
            $("#gridlist").jqGrid({
                url: "dicDrugformList.html",
                mtype: "POST",
                datatype: "json",
                postData:{"formId":"${formId}"},
                autowidth: true,
                height: 270,
                colNames: ['<spring:message code="message.jqGrid.cz"/>',
                           'formId',
                           '<spring:message code="message.listForm.yiJiJiXingBianMa"/>',
                           '<spring:message code="message.listForm.yiJiJiXingMingCheng"/>',
                           '<spring:message code="message.listForm.erJiJiXingBianMa"/>',
                           '<spring:message code="message.listForm.erJiJiXingMingCheng"/>',
                           '<spring:message code="message.lable.status"/>'
                           ],
                colModel: [
					
					{ name: 'operate', width: 50,sortable: false, align:"left",
						formatter : function(val, opts, rowdata) {
							var formCode = rowdata.formCode||rowdata.fatherCode.formCode;
							var formName = rowdata.formName||rowdata.fatherCode.formName;
							var str =  "<ms:btn url='/dicDrugform/edit.html'><a href='javascript:edit(\""+rowdata.formId+"\",\""+rowdata.formCode+"\")' style='background:url(${ctx}/lib/img/icon/icon.png) no-repeat 0px 0px' class='opIcon' title='<spring:message code="message.jqGrid.cz.bianJi"/>'></a></ms:btn>";
							    str += "<ms:btn url='/dicDrugformLog/log.html'><a href='${ctx}/dicDrugformLog/log.html?formId="+rowdata.formId+"&formCode="+formCode+"&formName="+formName+"' style='background:url(${ctx}/lib/img/icon/icon.png) no-repeat -120px 0px' class='opIcon' title='<spring:message code="message.jqGrid.cz.weiHuJiLu"/>'></a></ms:btn>";
							   if(!rowdata.formCode ){							    
								  str += "<ms:btn url='/dicDrugform/addTwo.html'><a href='javascript:addTwoForm(\""+rowdata.formId+"\",\""+rowdata.fatherCode.formCode+"\",\""+rowdata.fatherCode.formName+"\")'  style='background:url(${ctx}/lib/img/icon/icon.png) no-repeat -140px 0px' class='opIcon' title='<spring:message code="message.jqGrid.cz.xinZengErJiJiXing"/>'></a></ms:btn>";
							   }
								  //str+="<div id='"+rowdata.formId+"d' onmouseover=\"over('"+rowdata.formId+"','"+rowdata.formCode+"','"+rowdata.fatherCode.formCode+"','"+rowdata.fatherCode.formName+"');\"  style='height:24px;width:50px;margin:0 ;padding:0 ;float:left;line-height:24px'><a>更多...</a></div></div>";
					    	return str;
						}
					},
                    { name: 'formId',hidden:true, width: 80,align:"center",sortable: false},
                    { name: 'fatherCode.formCode', width: 80,align:"center",sortable: false,formatter:function(cellvalue, options, row){if(row.fatherCode != null){ return row.fatherCode.formCode; }else{ return row.formCode; }}},
                    { name: 'fatherCode.formName', width: 120,align:"center",sortable: false,formatter:function(cellvalue, options, row){if(row.fatherCode!=null){ return row.fatherCode.formName; }else{ return row.formName; }} },
                    { name: 'formCode', width: 100,align:"center",sortable: false,formatter:function(cellvalue, options, row){if(row.fatherCode!=null){ return row.formCode; }else{ return ""; }} },
                    { name: 'formName', width: 120,align:"center",sortable: false ,formatter:function(cellvalue, options, row){if(row.fatherCode!=null){ return row.formName; }else{ return ""; }} },
                    { name: 'status', width: 40 , align: 'center',sortable: false,
                    	cellattr:function (rowId, val, rawObject, cm) {
                            if(val == '停用'){
                                return 'style="color:red"';
                            }
                        },
                    	formatter : function(val, opts, rowdata) {
                    		var strSta = "";
                    		if(rowdata.status == 1){
                    			strSta += "<spring:message code="message.button.start"/>";
                    		}else{
                    			strSta += "<spring:message code="message.button.stop"/>";
                    		}
                    		return strSta;
                    	}
                    }
                    
                ],
                rowNum: 20,
                rowList: [10, 20, 50,100],
                rownumbers: true,
                pager: "#gridpage",
                viewrecords: true,
                multiselect: true, 
                gridComplete:function(){
                	//序号列宽度自适应（参数为jqgridID）
                    autoRNWidth("gridlist");
                	//数据只有一条时默认选中（参数为jqgridID）
                    selectByOneData("gridlist");
                },
                
                caption: "<spring:message code="message.list.drugformDiclist"/>",
                jsonReader: {
                    repeatitems: false,
                    id: "formId"
                }
            });
            $("#gridlist").jqGrid('navGrid', '#gridpage', { add: false, edit: false, del: false, search: false, refresh: false });
            
           
        });
        //保存
        function save(){
        	var fformCode="";
        	var fformName="";
        	var formCode = "";
	        var formName = "";
        	if(type==1){
        		if(flag){
        			if (!validate2()) {
    	    			return;
    	    		}
        			 fformCode = $("#txtOneID").val();
			         fformName = $("#txtOneName").val();
			         formCode = $("#txtTwoId").val();
			         formName = $("#txtTwoName").val();
			         if(fformCode==formCode){
			        	 $.alert("<spring:message code="message.yanZheng.liangGe"/><spring:message code="message.yanZheng.bianMa"/><spring:message code="message.yanZheng.buNengChongFu"/>", 'warn');
			        	 return;
			         }
			         if(fformName==formName){
			        	 $.alert("<spring:message code="message.yanZheng.liangGe"/><spring:message code="message.yanZheng.mingChen"/><spring:message code="message.yanZheng.buNengChongFu"/>", 'warn');
			        	 return;
			         }
        		}else{
        			if (!validate1()) {
    	    			return;
    	    		}
        			
        			 formCode = $("#txtOneID").val();
			          formName = $("#txtOneName").val();
        		}
        	}
			if(type==2){
			       if(editType==1){
			    	   if (!validate1()) {
	    	    			return;
	    	    		}
			    	   formCode = $("#txtOneID").val();
			           formName = $("#txtOneName").val();
			       }
				   if(editType==2){
					   if (!validate2()) {
	    	    			return;
	    	    		}
					   formCode = $("#txtTwoId").val();
			           formName = $("#txtTwoName").val();
			       }
			}
			if(type==3){
				if (!validate2()) {
	    			return;
	    		}
				
				formCode = $("#txtTwoId").val();
		        formName = $("#txtTwoName").val();
			}
			 var formId = $("#spid").val();
        	 var fatherId = $("#spfatherid").val();
        	 
      	  		$.HN.message.confirm('<spring:message code="message.HN.alert.OkSaveYESorNO"/>', '', '').on(function (e) {
	               if (e) {
	            	   
	            	   var url = "${ctx}/dicDrugform/add.html";
	            	    if(formId !=null && formId != ""){
	                       url = "${ctx}/dicDrugform/edit.html";
	                       }
	                        
	            	   
	               	$.ajax({
	           		   type: "POST",
	           		   url: url,
	           		   //data: "formCode="+formCode+"&formName="+formName+"&formId="+formId+"&fatherId="+fatherId,
	           		   data:{
	           			    formCode:formCode,
	           			    formName:formName,
	           			    formId:formId,
	           			    fatherId:fatherId,
	           			    fformCode:fformCode,
	           			    fformName:fformName
	           		   },
	           		   dataType:"json",
	           		   success: function(result){
	           			   if(result.success){
	           				$.alert("<spring:message code="message.HN.alert.success"/>", "success");
	           				   $("#gridlist").jqGrid('setGridParam',{page:1}).trigger("reloadGrid");
	           				$('#exampleModal').modal('hide');
	           			   }else{
	           				$.alert( result.msg, "error");
	                			   }
	                		   }
	                	});
	                }
           	 });
        }
        //编辑
        function edit(formId,formCode){
        	type=2;
        	$("#txtOneID").attr("readonly",false);
        	$("#txtOneName").attr("readonly",false);
        	$("#addTwoForm").hide();
        	$('.modal-title').html('<spring:message code="message.lable.DICjiXingBianJi"/>');
        	$("#spfatherid").val("");
        	$("#spid").val(formId);
        	$('#exampleModal').modal({
                keyboard: true
            });
        		$.post("${ctx}/dicDrugform/initEdit.html", {
        			formId : formId
        		},
        		 		
        		function(result) {
        			if (result.success) {
        				var obj = result.rows;
        				if(obj[0]){
        					$("#txtOneID").val(obj[0]);
            				$("#txtOneName").val(obj[1]);
            				$("#txtTwoId").val(obj[2]);
            				$("#txtTwoName").val(obj[3]);
            				$("#twoForm").show();
            				$("#txtOneID").attr("readonly",true);
            	        	$("#txtOneName").attr("readonly",true);
            	        	editType=2;
        				}else{
        					$("#txtOneID").val(obj[2]);
            				$("#txtOneName").val(obj[3]);
            				$("#txtTwoId").val("");
            				$("#txtTwoName").val("");
            				$("#twoForm").hide();
            				editType=1;
        				}
        				
        			}else{
        				$("#txtOneID").val("");
        				$("#txtOneName").val("");
        				$("#txtTwoId").val("");
        				$("#txtTwoName").val("");
        				$("#twoForm").hide();
        				$.alert("<spring:message code="message.HN.alert.fail"/>", "error");
        			}
        			
        		}, "json");
        }
        //添加二级剂型
        function addTwoForm(fatherId,formCode,formName){
        	type=3;
        	$("#txtOneID").attr("readonly",true);
        	$("#txtOneName").attr("readonly",true);
        	$("#addTwoForm").hide();
        	$("#twoForm").show();
        	$('.modal-title').html('<spring:message code="message.listForm.tianJiaErJiJiXing"/>');
        	$("#txtOneID").val(formCode);
        	$("#txtOneName").val(formName);
        	$('#txtTwoId').val("");
			$('#txtTwoName').val("");
        	$("#spid").val("");
        	$("#spfatherid").val(fatherId);
        	$('#exampleModal').modal({
                keyboard: true
            });
        }
        //查询
        function likeQuery(page){
        	if (page == ""){
        		page = 1;
        	}
        	var formCode  = $("#txtDrugName").val();
       	    var formName = $("#txtDrugForm").val();
       	    var status = $("#status").val();
       		 $("#gridlist").jqGrid('setGridParam', { 
       			url: "${pageContext.request.contextPath}/dicDrugform/dicDrugformList.html", 
       			mtype: "POST",
       		    datatype:'json',
		        postData:{
					"formCode":formCode,
					"formName":formName,
					"status":status},
       			page: page
       		}).trigger("reloadGrid");
        }
        
        //鼠标移入
         
         function validate1() {
     		var formCode = $.trim($("#txtOneID").val());
     		if (formCode == null || formCode == "") {
     			$.alert("<spring:message code="message.yanZheng.qingShuRu"/><spring:message code="message.yanZheng.yiJi"/><spring:message code="message.listForm.JiXingBianMa"/><spring:message code="message.yanZheng.ganTanHao"/>", "warn");
     			return false;
     		} else if(formCode.length>10){
     			$.alert("<spring:message code="message.yanZheng.yiJi"/><spring:message code="message.listForm.JiXingBianMa"/><spring:message code="message.yanZheng.changDuGuoChang"/>", "warn");
     			return false;
     		}
     		
     		var formName = $.trim($("#txtOneName").val());
     		if (formName == null || formName == "") {
     			$.alert("<spring:message code="message.yanZheng.qingShuRu"/><spring:message code="message.yanZheng.yiJi"/><spring:message code="message.listForm.JiXingMingCheng"/><spring:message code="message.yanZheng.ganTanHao"/>", "warn");
     			return false;
     		} else if(formName.length>100){
     			$.alert("<spring:message code="message.yanZheng.yiJi"/><spring:message code="message.listForm.JiXingMingCheng"/><spring:message code="message.yanZheng.changDuGuoChang"/>","warn");
     			return false;
     		}

     		return true;
     	}
     	function validate2() {
    		var formCode = $.trim($("#txtTwoId").val());
    		if (formCode == null || formCode == "") {
    			$.alert("<spring:message code="message.yanZheng.qingShuRu"/><spring:message code="message.listForm.erJiJiXingBianMa"/><spring:message code="message.yanZheng.ganTanHao"/>", "warn");
    			return false;
    		} else if(formCode.length>10){
     			$.alert("<spring:message code="message.listForm.erJiJiXingBianMa"/><spring:message code="message.yanZheng.changDuGuoChang"/>", "warn");
     			return false;
     		}
    		
    		var formName = $.trim($("#txtTwoName").val());
    		if (formName == null || formName == "") {
    			$.alert("<spring:message code="message.yanZheng.qingShuRu"/><spring:message code="message.listForm.erJiJiXingMingCheng"/><spring:message code="message.yanZheng.ganTanHao"/>", "warn");
    			return false;
    		} else if(formName.length>100){
     			$.alert("<spring:message code="message.listForm.erJiJiXingMingCheng"/><spring:message code="message.yanZheng.changDuGuoChang"/>", "warn");
     			return false;
     		}

    		return true;
    	}
     	//添加二级剂型
     	$("#addDouble").click(function(){
     		 if (!validate1()) {
	    			return;
	    	}
     		$.post("${ctx}/dicDrugform/checkRepeat.html", {
				formCode : $("#txtOneID").val(),
				formName : $("#txtOneName").val()
			}, function(result) {
				if (result.success) {
					flag = true;
					$("#twoForm").show();
					$("#addTwoForm").hide();
					$("#txtOneID").attr("readonly",true);
		        	$("#txtOneName").attr("readonly",true);
				} else {
					$.alert(result.msg||"<spring:message code="message.HN.alert.fail"/>", "error");
				}
			}, "json");
     	});
     	
     	function exportExcel(){
     		$.HN.message.confirm('<spring:message code="message.lable.OKExport"/>&nbsp;<spring:message code="message.lable.drugformxls"/>&nbsp;<spring:message code="message.lable.ma"/>', '', '').on(function (e) {
	               if (e) {
	            	  $("#exportExcel").submit();
			
	               }
          	 }); 
          	
     	}
    </script>
</body>
</html>